<template>
  <div class="">
		<Form :model="OvulationDetail" label-position="left" :label-width="50">
	        
	        <Form-item label="日期">
	            <Row>
	           		 <Col span="24">
	                    <Date-picker type="date" placeholder="请输入排卵日期" v-model="OvulationDetail.date"></Date-picker>
	                </Col>
	            </Row>
        	</Form-item>


        	<Form-item label="左边卵巢">
	            <Row>
	           		 <Col span="16">
	                     <Input v-model="OvulationDetail.leftText" ></Input>
	                </Col>
	                <Col span="2" style="text-align: center">-</Col>
               		 <Col span="6">
                		<Button type="ghost" class="pingButton" @click="addLeftItems">添加</Button>
                	</Col>
	            </Row>
	            
	                
        	</Form-item>


          <Form-item
            v-for="(item, index) in OvulationDetail.leftItems"
            :key="index">
            <Row>
                 <Col span="16">
                       <Input type="text" v-model="item.value"> </Input>
                  </Col>
            </Row>
        </Form-item>



        	<Form-item label="右边边卵巢">
	            <Row>
	           		 <Col span="16">
	                     <Input v-model="OvulationDetail.rightText" ></Input>
	                </Col>
	                <Col span="2" style="text-align: center">-</Col>
               		 <Col span="6">
                		<Button type="ghost" class="pingButton" @click="addRightItems">添加</Button>
                	</Col>
	            </Row>
	          
	                
        	</Form-item>

          <Form-item
            v-for="(item, index) in OvulationDetail.rightItems"
            :key="index">
            <Row>
                 <Col span="16">
                       <Input type="text" v-model="item.value"> </Input>
                  </Col>
            </Row>
         </Form-item>



			<upload-helper  
				type="ovl"
				@handleSuccess="handleSuccess"
				v-bind:defaultList="OvulationDetail.rusultlist" 
				LeftTitle="B超检查结果" 
				v-bind:selectType="bodyType">
			</upload-helper>

        </Form>
  </div>
</template>
<script>
import UploadHelper from '../components/UploadHelper'
import Store from '../store'
import api from '../js/axios-module'
import Vue from 'vue'
export default {

  data () {
    return {
    	OvulationDetail:{
        id:"",
    		date:'',
    		leftText:"",
    		leftItems:[],
    		rightText:"",
    		rightItems:[],
    		rusultlist:[],
    	},
    	
        bodyType:""
       
    }
  },
  components:{UploadHelper},
  methods:{
    onChangeLeftItems:function(index){
     
     
    },
    onChangeRightItems:function(index){

    },
  	addLeftItems:function(){
  		if (this.OvulationDetail.leftText != '') {
  			this.OvulationDetail.leftItems.push({
          value:this.OvulationDetail.leftText
        });
  			this.OvulationDetail.leftText="";
  		}
  	},
  	addRightItems:function(){
  		if (this.OvulationDetail.rightText != '') {
  			this.OvulationDetail.rightItems.push({
          value:this.OvulationDetail.rightText
        });
  			this.OvulationDetail.rightText="";
  		}
  	},openItem:function(number){

  		// alert(number)
  		// -1的时候为新增
  		if (number == -1) {
    		this.OvulationDetail.date = new Date()
  	  	this.OvulationDetail.dateStr = "";
  	  	this.OvulationDetail.leftItems=[] ;
  			this.OvulationDetail.rightItems= [];
  			this.OvulationDetail.rusultlist = [];
  		}else{
  			//获取数据库的数据

  			  var data = this.OvulationInduction.data[number];
  	  		this.OvulationDetail.date = data.date;
  	  		this.OvulationDetail.dateStr = data.dateStr;
  	  		this.OvulationDetail.leftItems= data.leftItems ;
  			  this.OvulationDetail.rightItems= data.rightItems;
  			  this.OvulationDetail.rusultlist = data.rusultlist;
          this.OvulationDetail.ovulationid= data.ovulationid
    
  		}
  		

  		 

  	}
  	,updateItem:function(number){
  // -1的时候为新增
  		if (number == -1) {

  		
  			if (this.OvulationDetail.date instanceof Date) {

          console.log("新增")
  				var date = this.OvulationDetail.date.format('yyyy-MM-dd');
  			
	  			
          var item =  new Object();
          item.date = date;
          item.data = [];
          item.data[0] = this.OvulationDetail.leftItems;
          item.data[1] = this.OvulationDetail.rightItems;
          item.file = this.OvulationDetail.rusultlist;
    


          api.setOvulationDetail(item,'post').then(response=>{

            if(response.data.code==200){

              this.$Message.success("提交成功")
       
              this.OvulationInduction.data.push(
                {"ovulationid":response.data.id,
                 "dateStr":date,
                 "date":this.OvulationDetail.date,
                 "leftItems":this.OvulationDetail.leftItems,
                 "rightItems":this.OvulationDetail.rightItems,
                 "rusultlist": this.OvulationDetail.rusultlist
                }
              );

            }
          },err=>{
               this.$Message.error("提交失败")
          })


  			}else{
  				this.$Message.error('请填完整数据');
  			}
  		}
  		else{
        console.log("修改")
        var cell = new Object()

        cell.ovulationid = this.OvulationDetail.ovulationid;

        if (this.OvulationDetail.date instanceof Date) {
            cell.date = this.OvulationDetail.date.format("yyyy-MM-dd")

        }else{
            cell.date = this.OvulationDetail.date
        }


        cell.data = [];
        cell.data[0] = this.OvulationDetail.leftItems;
        cell.data[1] = this.OvulationDetail.rightItems;
        cell.file = this.OvulationDetail.rusultlist;

  
        api.setOvulationDetail(cell,'patch').then(response=>{
            if(response.data.code==200){
              this.$Message.success("提交成功")

              var item = this.OvulationInduction.data[number]
              item.date = cell.date;
              // item.dateStr = str;
              item.leftItems=this.OvulationDetail.leftItems  ;
              item.rightItems= this.OvulationDetail.rightItems;
              item.rusultlist = this.OvulationDetail.rusultlist;
   
            }
        },err=>{
          this.$Message.error('失败,请稍后再尝试');
        })

  			
  			
  		}


  		Store.save(this.OvulationInduction,Store.OVULATIONINDUCTION_KEY);



  	},handleSuccess:function(res,file,selectType){
      
      var message = file.response.data
  		this.OvulationDetail.rusultlist.push({
	        url : message.url,
	        name : message.url,
          create_at : baseURL+message.create_at
	    })
  	}

  },props:['OvulationInduction'],
  watch:{
   'OvulationDetail.leftItems':{
     handler:function(val,oldval){
        
         

     },
     deep:true
   },
   'OvulationDetail.rightItems':{
     handler:function(val,oldval){
         
   


     },
     deep:true
   }


  },
}
</script>
